﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;

namespace TM.Unofficial.Chat
{
    //Chat Control System
    public class ChatClient
    {
        public List<ChatWindow> ChatWindows {get; private set;}

        Unofficial.Network.VBrowser browser;

        /// <summary>
        /// initializes a new chatclient
        /// </summary>
        /// <param name="browser">webbrowser</param>
        public ChatClient(Unofficial.Network.VBrowser browser)
        {
            //init
            this.browser = browser;
            ChatWindows = new List<ChatWindow>();
        }

        /// <summary>
        /// Opens a new chat window with a user
        /// </summary>
        /// <param name="username">Username of the partner</param>
        /// <returns>List Index ID</returns>
        public int OpenChat(string username)
        {
            //find user id
            TM.API.ThatsMe tm = new API.ThatsMe();
            TMObjects.Member chatpartner = tm.getMemberByNick(username);

            //Try to open a chat
            string xmlresult = browser.PostRequest(new Uri("http://thats-me.ch/tm-im-new/ajax_getchatwindow.php"), Encoding.UTF8.GetBytes("to=" + chatpartner.ID + "&inbg=1"), false);

            //Maybe do something with it <--!!

            //Create the chatwindow interface
            ChatWindow cw = new ChatWindow(this.browser, chatpartner);
            ChatWindows.Add(cw);

            //return chatwindow index
            return ChatWindows.IndexOf(cw);
        }

        /// <summary>
        /// Closese a chat window
        /// </summary>
        /// <param name="id">List Index ID</param>
        public void CloseChat(int id)
        {
            //Try to close a chat
            string xmlresult = browser.PostRequest(new Uri("http://thats-me.ch/tm-im-new/ajax_closechat.php"), Encoding.UTF8.GetBytes("to=" + id.ToString()), false);
            string xmlresults = browser.PostRequest(new Uri("http://thats-me.ch/tm-im-new/ajax_closechat.php"), Encoding.UTF8.GetBytes("to=" + this.ChatWindows[id].ChatPartner.ID), false);
        }

        /// <summary>
        /// Loads every chat (initialize them)
        /// </summary>
        public void LoadCurrentChats()
        {
            //try to get every chat window (maybe for initialising)
            string xmlresult = browser.GetRequest(new Uri("http://thats-me.ch/tm-im-new/ajax_update.php"), false);

            //parse result
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.LoadXml(xmlresult);

			//this.lastupdate_id = Convert.ToInt32(xmldoc.GetElementsByTagName("lastid")[0].InnerText);
			XmlNodeList chats = xmldoc.GetElementsByTagName("chats");
			
			foreach(XmlNode node in chats)
			{
				//Name noch richtig eingeben
				XmlNode tonode = node.SelectSingleNode("to");
				ChatWindow wdc = new ChatWindow(this.browser, new TM.API.ThatsMe().getMemberById(Convert.ToInt32(tonode.InnerText)));
				ChatWindows.Add(wdc);
			}

			//its just a workaround because jo doesnt have a "GetNickNameById" Method
//			TM.API.ThatsMe tm = new TM.API.ThatsMe();
// 			string xmlresult = browser.GetRequest(new Uri("http://thats-me.ch/tm-im-new/ajax_allchats.php"), false);
//           	
//			MatchCollection matches = Regex.Matches(xmlresult, @"\S?members/[A-Za-z0-9]*\S?", RegexOptions.Multiline);
//			
//			foreach(Match m in matches)
//			{
//				//cut the string
//				string tempuser = m.Value.Remove(0, m.Value.IndexOf("/", 1) + 1);
//				tempuser = tempuser.Remove(tempuser.IndexOf("/"));
//				
//				this.OpenChat(tempuser);
//			}
        }
    }
}
